package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.TextView;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.MyApplySimpleInfo;
import com.pj.wisdomlogistics.mvp.contract.MyTransactContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.MyTransactPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.MyApplySimpleAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.LoadMoreRecyclerView;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class BusinessListActivity extends MvpActivity<MyTransactPresenter> implements MyTransactContract.View, MyApplySimpleAdapter.onItemClick, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.recyclerview)
    LoadMoreRecyclerView mRecyclerview;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;

    private int pageIndex = 1;
    private int pageCount = 10;
    private MyApplySimpleAdapter mAdapter;
    private int content;
    private boolean isFirst = true;
    private int totalCount;
    private int totalPage;


    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
    }

    @Override
    public void showSuccess(MyApplySimpleInfo mInfo) {
        dismissProgressDialog();

        if (mSwipeRefresh.isRefreshing()) {
            mSwipeRefresh.setRefreshing(false);
        }

        if (isFirst) {
            if(mInfo == null || mInfo.getList().size() == 0){
                mAdapter.removeAll();
                return;
            }
            //计算是否需要footview
            totalCount = mInfo.getCount();
            if (pageCount < totalCount) {
                totalPage = totalCount % pageCount == 0 ? totalCount / pageCount : totalCount / pageCount + 1;
            } else {
                totalPage = 1;
            }
            mRecyclerview.setAutoLoadMoreEnable(pageIndex < totalPage);
            isFirst = false;

            mAdapter.setList(mInfo.getList());
        }else {
            //上拉加载
            mAdapter.appendList(mInfo.getList());
        }
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.text_blue));

        content = 0;
        String title = null;
        switch (getIntent().getIntExtra(Cons.Business.BUSINESS , -1)) {
            case 1:
                content = 3;
                title = "会议室申请";
                break;
            case 2:
                content = 2;
                title = "用车申请";
                break;
            case 3:
                content = 4;
                title = "维护报修";
                break;
            case 4:
                content = 5;
                title = "用餐申请";
                break;
        }
        mTvTitle.setText(title);

        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyApplySimpleAdapter(this);
        mRecyclerview.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mRecyclerview.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                mPresenter.myTodo(MyApplication.userID, content , 0, 1, ++pageIndex, pageCount);
                mRecyclerview.notifyMoreFinish(pageIndex < totalPage);
            }
        });

        mSwipeRefresh.setColorSchemeResources(
                R.color.google_orange,
                R.color.google_yellow,
                R.color.google_blue,
                R.color.google_green
        );

        mSwipeRefresh.setOnRefreshListener(this);

        mPresenter.myTodo(MyApplication.userID , content , 0 , 1 , pageIndex , pageCount);
        showProgressDialog();

    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_business_list;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    public void itemClick(int type, int id) {
        Intent mIntent = null;
        switch (type) {
            case Cons.ApplyType.APPLY_GWYC:
                mIntent = new Intent(this, CarDetailActivity.class);
                mIntent.putExtra(Cons.CarInfo.CAR_TRANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_HYS:
                mIntent = new Intent(this, ApplyDetailActivity.class);
                mIntent.putExtra(Cons.TransactDetail.MEETING_TRANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_ST:
                mIntent = new Intent(this , MealDetialActivity.class);
                mIntent.putExtra(Cons.MealInfo.MEAL_RANSACT_ID , id);
                break;
            case Cons.ApplyType.APPLY_WHBX:
                mIntent = new Intent(this, RepairDetailActivity.class);
                mIntent.putExtra(Cons.RepairInfo.REPAIR_RANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_WMDC:
                break;
        }
        mIntent.putExtra(Cons.ApplyDetail.MEETING_ID, id);
        startActivity(mIntent);
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        isFirst = true;
        mPresenter.myTodo(MyApplication.userID, content ,0, 1, pageIndex, pageCount);
    }
}
